#include "confirmquit.h"

ConfirmQuit::ConfirmQuit(GameInfo& gameInfo) : _gameInfo(gameInfo),
    _gui(*new GuiSystem()), _view(*new ConfirmQuitView(*this, _gui))
{
    //ctor
}

ConfirmQuit::~ConfirmQuit()
{
    delete &_gui;
    delete &_view;
}

bool ConfirmQuit::update(float dt, const Input& input)
{
    GuiId button;
    if (_gui.interact(input, &button, (int*)NULL)) {
        if (button == GUI_YES) {
            _gameInfo.quit();
            return false;
        } else if (button == GUI_NO) {
            return false;
        }
    }

    return true;
}

IGameStateView& ConfirmQuit::getView()
{
    return _view;
}

void ConfirmQuit::onEnter()
{

}

ConfirmQuitView::ConfirmQuitView(ConfirmQuit& model, GuiSystem& gui) :
    _model(model), _gui(gui)
{
    _gui.addStatic(0, 0.3f, 0.45f, 0.4f, 0.25f,
        *new Texture("..\\..\\models\\textures\\prom_bg.png"));

    _gui.addStatic(1, 0.37f, 0.6f,
        _gui.widthToRel(256), _gui.heightToRel(64),
        *new Texture("..\\..\\models\\textures\\text_confirmquit.png", true));

    _gui.addButton(GUI_YES, 0.33f, 0.51f,
        _gui.widthToRel(154), _gui.heightToRel(38),
        *new Texture("..\\..\\models\\textures\\yes_button.png", true));

    _gui.addButton(GUI_NO, 0.52f, 0.51f,
        _gui.widthToRel(154), _gui.heightToRel(38),
        *new Texture("..\\..\\models\\textures\\no_button.png", true));

}

void ConfirmQuitView::draw(bool clear)
{
    setup2dView();

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glColor4f(0.5, 0.5f, 0.5f, 0.5f);
    glBegin(GL_QUADS);
        glVertex2f(0.0f, 0.0f);
        glVertex2f(1.0f, 0.0f);
        glVertex2f(1.0f, 1.0f);
        glVertex2f(0.0f, 1.0f);
    glEnd();

    glDisable(GL_BLEND);

    _gui.draw();
}

void ConfirmQuitView::onEnter()
{

}

void ConfirmQuitView::setup2dView()
{
    glDisable(GL_CULL_FACE);
    glDisable(GL_LIGHTING);
    glDisable(GL_DEPTH_TEST);
    //glEnable(GL_TEXTURE_2D);

    //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0f, 1.0f, 0.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

}
